package cc.mrbird.febs.power.controller;


import cc.mrbird.febs.common.utils.R;
import cc.mrbird.febs.power.entity.OrgBindAccountInfo;
import cc.mrbird.febs.power.entity.OrgInfo;
import cc.mrbird.febs.power.service.IOrgBindAccountInfoService;
import cc.mrbird.febs.power.service.IOrgInfoService;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author MrBird
 */
@RestController
@RequestMapping("/power/org-info")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class OrgInfoController {

    private final IOrgInfoService iOrgInfoService;

    private final IOrgBindAccountInfoService iOrgBindAccountInfoService;

    private SimpleDateFormat sdf3=new SimpleDateFormat("yyyy-MM-dd");

    /**
     * 分页获取组织
     * @param page
     * @param orgInfo
     * @return
     */
    @GetMapping("/page")
    public R getOrgInfoByPage(Page page,OrgInfo orgInfo) {
        return R.ok(iOrgInfoService.getOrgInfoByPage(page,orgInfo));
//        return R.ok(iOrgInfoService.page(page, Wrappers.<OrgInfo>lambdaQuery()
//                .like(orgInfo.getOrgName()!=null,OrgInfo::getOrgName,orgInfo.getOrgName())
//                .gt(orgInfo.getCreateDate()!=null,OrgInfo::getCreateDate,orgInfo.getCreateDate())));
    }

    /**
     * 获取所有组织机构
     * @return
     */
    @GetMapping
    public R getOrgInfo() {
        return R.ok(iOrgInfoService.list());
    }

    /**
     * 新增组织机构
     * @param orgInfo
     * @return
     */
    @PostMapping("/OrgInfoAdd")
    public R OrgInfoAdd(OrgInfo orgInfo,String accountId) {
        if(orgInfo.getParentOrgId()==null || ("").equals(orgInfo.getParentOrgId()) ){
            orgInfo.setParentOrgId(new Long((long)0));
        }
        orgInfo.setCreateDate(sdf3.format(new Date()));
        iOrgInfoService.save(orgInfo);
        if(accountId!=null && !("").equals(accountId)){
            iOrgBindAccountInfoService.save(new OrgBindAccountInfo(orgInfo.getOrgId(),accountId));
        }
        return R.ok("ok");
    }

    /**
     * 获取详细信息
     * @param id
     * @return
     */
    @GetMapping("/info/{id}")
    public R getMessageByOrgId(@PathVariable("id") Integer id) {
        return R.ok(iOrgBindAccountInfoService.getOne(Wrappers.<OrgBindAccountInfo>lambdaQuery().eq(OrgBindAccountInfo::getOrgId,id)).getAccountId().split(","));
    }

    /**
     * 修改组织机构
     * @param orgInfo
     * @return
     */
    @PutMapping("/OrgInfoEdit")
    public R OrgInfoEdit(OrgInfo orgInfo,String accountId) {
        if(accountId!=null && !("").equals(accountId)){
            iOrgBindAccountInfoService.update(Wrappers.<OrgBindAccountInfo>lambdaUpdate().set(OrgBindAccountInfo::getAccountId,accountId).eq(OrgBindAccountInfo::getOrgId,orgInfo.getOrgId()));
        }
        return R.ok(iOrgInfoService.updateById(orgInfo));
    }

    /**
     * 通过ID删除记录
     * @param id
     * @return
     */
    @PostMapping("/OrgInfoDel")
    public R OrgInfoDel(@PathVariable Integer id) {
        return R.ok(iOrgInfoService.removeById(id));
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @DeleteMapping("/man/{ids}")
    public R OrgInfosDel(@PathVariable("ids") List<Integer> ids) {
        return R.ok(iOrgInfoService.removeByIds(ids));
    }


}
